import { useRef, useEffect } from "react";
// 自定义高度输入框
export const useAutoResizeTextArea = (maxHeight = 350) => {
  const textareaRef = useRef<HTMLTextAreaElement | null>(null); // 添加类型参数

  useEffect(() => {
    const textarea = textareaRef.current;
    const adjustHeight = () => {
      if (textarea) {
        // 确保 textarea 存在
        textarea.style.height = "auto";
        textarea.style.height = `${Math.min(
          textarea.scrollHeight,
          maxHeight
        )}px`;
      }
    };

    textarea?.addEventListener("input", adjustHeight); // 使用可选链
    return () => textarea?.removeEventListener("input", adjustHeight); // 使用可选链
  }, [maxHeight]);

  return textareaRef;
};
